package com.um.springboot.starter.orm.params;

import com.um.springboot.starter.orm.entity.RangeEntity;
import com.um.springboot.starter.utils.StringUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
 * @author 邓南辉
 * 日期 2023/2/7
 * 版权所有 广州优匠科技有限公司
 */
@Data
public class ListParams implements Serializable {

    protected Map<String,String> condition = new Hashtable<>();

    protected CustomParams custom = new CustomParams();

    public String[] getProperties(){
        List<String> properties = new ArrayList<>();
        Iterator<String> iterator = condition.keySet().iterator();
        while(iterator.hasNext()){
            String name = iterator.next();
            if(name.equals("orderby"))
                continue;
            properties.add(name);
        }

        String[] list = new String[properties.size()];
        return properties.toArray(list);
    }

    public Object[] getValues(){
        List<Object> values = new ArrayList<>();
        Iterator<String> iterator = condition.keySet().iterator();
        while(iterator.hasNext()){

            String name = iterator.next();
            if(name.equals("orderby"))
                continue;

            values.add(condition.get(name));
        }

        Object[] list = new Object[values.size()];
        return values.toArray(list);
    }

    public String[] getOrderNames(){
        String orderby = StringUtil.getTrim(condition.get("orderby"));
        if(orderby.equals(""))
            return new String[]{};
        else{
            return new String[]{StringUtils.split(orderby," ")[0]};
        }
    }

    public String[] getSortOns(){
        String orderby = StringUtil.getTrim(condition.get("orderby"));
        if(orderby.equals(""))
            return new String[]{};
        else{
            return new String[]{StringUtils.split(orderby," ")[1]};
        }
    }

    public List<RangeEntity> getRangeEntitys(){
        List<RangeEntity> rangeEntities = new ArrayList<>();

        Iterator<String> iterator = custom.getRangeField().keySet().iterator();
        while(iterator.hasNext()){
            String name = iterator.next();
            List<RangeParams> rangeParamsList = custom.getRangeField().get(name);

            if(rangeParamsList.size() == 0)
                continue;;

            RangeEntity rangeEntity = new RangeEntity();
            rangeEntity.setName(name);
            for(RangeParams rangeParams : rangeParamsList){
                if(rangeParams.getRange().equals("begin")){
                    rangeEntity.setBeginValue(rangeParams.getValue());
                }else if(rangeParams.getRange().equals("end")){
                    rangeEntity.setEndValue(rangeParams.getValue());
                }
            }
            rangeEntities.add(rangeEntity);
        }

        return rangeEntities;
    }


}
